package xyz.xpman.prisonerfitness.entity;

import android.graphics.Bitmap;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import xyz.xpman.prisonerfitness.config.Constant;
import xyz.xpman.prisonerfitness.util.JsonUtil;

@SuppressWarnings("unused")
public class Moves {
    public static final String ID = "id";
    public static final String TITLE = "title";
    public static final String DESCRIPTION = "description";
    public static final String GUIDE_IMG = "img";

    private String id;
    private String cId;
    private String record;
    private String title = null;
    private String description = null;
    private Integer guideImgId = null;
    private String[] guideImg = new String[0];
    private Training training = null;



    public String getChapterId() {
        return cId;
    }

    public void setChapterId(@NotNull String cId) {
        this.cId = cId;
    }



    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getGuideImgId() {
        return guideImgId;
    }

    private void setGuideImgId(int guideImgId) {
        this.guideImgId = guideImgId;
    }

    public String[] getGuideImg() {
        return guideImg;
    }

    private void setGuideImg(@NotNull String[] guideImg) {
        if (null != guideImg) {
            this.guideImg = guideImg.clone();
        }
    }

    public boolean hasGuidImg() {
        return 0 != guideImg.length;
    }


    public Training getTraining() {
        return training;
    }

    private void setTraining(Training training) {
        this.training = training;
    }




    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    public static Moves byJson(@NotNull JSONObject json) {
        Moves moves = new Moves();

        try {
            JSONObject item = json.getJSONObject(Constant.GUIDE_KEY);

            moves.setId(json.getString(ID));
            moves.setTitle(item.getString(TITLE));
            moves.setDescription(item.getString(DESCRIPTION));


            JSONArray guideImgArray = item.getJSONArray(GUIDE_IMG);
            moves.setGuideImg(JsonUtil.toStringArray(guideImgArray));


            // training
            if (json.has(Constant.TRAINING_KEY)) {
                JSONObject training = json.getJSONObject(Constant.TRAINING_KEY);
                moves.setTraining(Training.byJson(training));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return moves;
    }


    public Bitmap getFlagImg() {
        return Mappings.ChaptersFlagImgMapping.getImg(getChapterId());
    }
}
